//
//  NewMainViewController.m
//  Doctor1.0
//
//  Created by mk on 15/11/2.
//  Copyright (c) 2015年 cocoa-mk. All rights reserved.
//

#import "NewMainViewController.h"
#import "NewMainCell.h"
#import "WebViewController.h"
#import "NewReservationListViewController.h"
#import "Doctor.h"
#import "LookingForDoctorViewController.h"

@interface NewMainViewController ()
{
    NSString * strdiseaseHeadUrl;
    NSString * strdoctorUrl;
    
    NSMutableArray * arrayBanners;
    DoctorListData * doctorList;
    NSMutableArray * arrTitle;//
}

@end

@implementation NewMainViewController


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[BaiduMobStat defaultStat] pageviewStartWithName:@"NewMainViewController"];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    [[BaiduMobStat defaultStat] pageviewEndWithName:@"NewMainViewController"];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (NSVersionNumber_iOS_7_0)
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        UIView * viewNav = [[UIImageView alloc] initWithFrame:CGRectMake(0, -64, appScreenWidth, 64)];
        viewNav.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:viewNav];
    }
    UILabel * titleNav = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 30)];
    titleNav.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:18];
    titleNav.backgroundColor = [UIColor clearColor];
    titleNav.textAlignment = NSTextAlignmentCenter;
    titleNav.textColor = defaultNavBackColor;
    [self.navigationItem setTitleView:titleNav];
    titleNav.text = @"名医主刀";
    
    if (doctorList.dataArray.count == 0)
    {
      [self getDoctorData];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
   // viewTop.width = tabMain.width;
    
    btnTitle1.layer.cornerRadius = 4;
    btnTitle2.layer.cornerRadius = 4;
    btnTitle3.layer.cornerRadius = 4;
    btnTitle4.layer.cornerRadius = 4;
    btnTitle5.layer.cornerRadius = 4;
    btnTitle6.layer.cornerRadius = 4;
    btnTitle1.layer.borderWidth = 1;
    btnTitle2.layer.borderWidth = 1;
    btnTitle3.layer.borderWidth = 1;
    btnTitle4.layer.borderWidth = 1;
    btnTitle5.layer.borderWidth = 1;
    btnTitle6.layer.borderWidth = 1;
    btnTitle1.layer.borderColor = UIColorFromRGB(0xf0, 0xf0, 0xf0).CGColor;
    btnTitle2.layer.borderColor = UIColorFromRGB(0xf0, 0xf0, 0xf0).CGColor;
    btnTitle3.layer.borderColor = UIColorFromRGB(0xf0, 0xf0, 0xf0).CGColor;
    btnTitle4.layer.borderColor = UIColorFromRGB(0xf0, 0xf0, 0xf0).CGColor;
    btnTitle5.layer.borderColor = UIColorFromRGB(0xf0, 0xf0, 0xf0).CGColor;
    btnTitle6.layer.borderColor = UIColorFromRGB(0xf0, 0xf0, 0xf0).CGColor;
    
    for (NSLayoutConstraint *constraints in [btnTitle1.superview constraints])
    {
        if (constraints.firstItem == btnTitle1 && constraints.firstAttribute == NSLayoutAttributeLeading &&
            constraints.secondItem == btnTitle1.superview && constraints.secondAttribute == NSLayoutAttributeLeading)
        {
            constraints.constant = ((appScreenWidth-240)/4);
            break;
        }
    }
    
    for (NSLayoutConstraint *constraints in [btnTitle3.superview constraints])
    {
        if (constraints.firstItem == btnTitle3.superview && constraints.firstAttribute == NSLayoutAttributeTrailing &&
            constraints.secondItem == btnTitle3 && constraints.secondAttribute == NSLayoutAttributeTrailing)
        {
            constraints.constant = ((appScreenWidth-240)/4);
            break;
        }
    }
    
    for (NSLayoutConstraint *constraints in [viewTop constraints])
    {
        if (constraints.firstItem == viewTop && constraints.firstAttribute == NSLayoutAttributeWidth)
        {
            constraints.constant = appScreenWidth;
            break;
        }
    }
    
    
    tabMain.tableHeaderView = viewTop;
    arrayBanners = [[NSMutableArray alloc]initWithCapacity:0];
    arrTitle = [[NSMutableArray alloc]initWithCapacity:0];
    doctorList = [[DoctorListData alloc]init];
}



//获取医生列表信息
-(void)getDoctorData
{
    [indV startAnimating];
    indV.hidden = NO;

    doctorList.urlPath = @"/appnav1";
    doctorList.strDataKey = @"doctors";
    
    [doctorList getLoadData:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        //NSLog(@"==%@",responseObject);
        NSDictionary * dicData = [responseObject objectForKey:@"results"];
        
        id data = [dicData objectForKey:@"banners"];
        if ([data isKindOfClass:[NSArray class]] && [data allObjects].count>0)
        {
           arrayBanners = data;
        }
        
        data = [dicData objectForKey:@"disNavs"];
        if ([data isKindOfClass:[NSArray class]] && [data allObjects].count>0)
        {
          arrTitle = data;
        }
        
        strdiseaseHeadUrl =  NullObjToString([dicData objectForKey:@"diseaseHeadUrl"]);
        strdoctorUrl =  NullObjToString([dicData objectForKey:@"doctorUrl"]);
        //NSLog(@"==%@===%@",strdiseaseHeadUrl,strdoctorUrl);
        [tabMain reloadData];
        [self setTopBannersScrollViewData];
        //[self setTitle];
        [indV stopAnimating];
        indV.hidden = YES;
  
    } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         [indV stopAnimating];
         indV.hidden = YES;
    }];
}

//设置title
-(void)setTitle
{
    if (arrTitle.count>0)
     [btnTitle1 setTitle:[[arrTitle objectAtIndex:0] objectForKey:@"name"] forState:UIControlStateNormal];
    if (arrTitle.count>1)
     [btnTitle2 setTitle:[[arrTitle objectAtIndex:1] objectForKey:@"name"] forState:UIControlStateNormal];
    if (arrTitle.count>2)
     [btnTitle3 setTitle:[[arrTitle objectAtIndex:2] objectForKey:@"name"] forState:UIControlStateNormal];
    if (arrTitle.count>3)
     [btnTitle4 setTitle:[[arrTitle objectAtIndex:3] objectForKey:@"name"] forState:UIControlStateNormal];
    if (arrTitle.count>4)
     [btnTitle5 setTitle:[[arrTitle objectAtIndex:4] objectForKey:@"name"] forState:UIControlStateNormal];
    if (arrTitle.count>5)
     [btnTitle6 setTitle:[[arrTitle objectAtIndex:5] objectForKey:@"name"] forState:UIControlStateNormal];
}


#pragma mark 设置TOPview
-(void)setTopBannersScrollViewData
{
    scrollTop.contentSize = CGSizeMake(arrayBanners.count * appScreenWidth, 120);
    pagev.numberOfPages = arrayBanners.count;
    
    imageDefu_top.hidden = (arrayBanners.count>0);
    
    for (int i = 0; i< arrayBanners.count; i++)
    {
        UIView * viewAll = [[UIView alloc]init];
        viewAll.frame = CGRectMake(0, i*appScreenWidth,appScreenWidth, scrollTop.height);
        [scrollTop addSubview:viewAll];
        
        NSDictionary * dicData = [arrayBanners objectAtIndex:i];
        NSString * sUrl = [NSString stringWithFormat:@"%@",[dicData objectForKey:@"imageUrl"]];
        UIImageView * imageicon = [[UIImageView alloc]init];
        imageicon.frame = viewAll.frame;
        imageicon.contentMode = UIControlContentHorizontalAlignmentFill;
        [imageicon sd_setImageWithURL:sUrl placeholderImage:[UIImage imageNamed:@""]];
       // [imageicon sd_setImageWithURL:sUrl placeholderImage:[UIImage imageNamed:@""] options:SDWebImageProgressiveDownload];
        
        [viewAll addSubview:imageicon];
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = imageicon.frame;
        btn.tag = i;
        btn.userInteractionEnabled = YES;
        [btn addTarget:self action:@selector(clicikBanners:) forControlEvents:UIControlEventTouchUpInside];
        [viewAll addSubview:btn];
        
    }
    
}

#pragma mark 点击广告
-(void)clicikBanners:(id)sender
{
    UIButton * btn = (UIButton *)sender;
    NSDictionary * dicData = [arrayBanners objectAtIndex:btn.tag];
    
    NSString * sUrl = [NSString stringWithFormat:@"%@",[dicData objectForKey:@"actionUrl"]];
    NSString * sTitle = [NSString stringWithFormat:@"%@",[dicData objectForKey:@"pageTitle"]];
    
    WebViewController * view = [[WebViewController alloc]initWithNibName:@"WebViewController" bundle:nil];
    view.strTitle = sTitle;
    view.strUrl = sUrl;
    [self.navigationController pushViewController:view animated:YES];
    
}



-(IBAction)btnTitle:(id)sender
{
    if (doctorList.dataArray.count > 0)
    {
        UIButton * btn = (UIButton *)sender;
        LookingForDoctorViewController * look = [[LookingForDoctorViewController alloc]initWithNibName:@"LookingForDoctorViewController" bundle:nil];
        look.arrayTitle = arrTitle;
        look.selectFatherID = btn.tag;
        look.strDoctorUrl = strdoctorUrl;
        look.strDiseaseHeadUrl = strdiseaseHeadUrl;
        [self.navigationController pushViewController:look animated:YES];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return doctorList.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 106;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellTableIdentifier = @"NewMainCellStr";
    
    NewMainCell *cell = (NewMainCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
    if (!cell)
    {
        cell = (NewMainCell *)[[[NSBundle mainBundle] loadNibNamed:@"NewMainCell" owner:self options:nil] objectAtIndex:0];
    }
    cell.celldelegate = self;
    [cell fileDoctorDataWithDic:[doctorList.dataArray objectAtIndex:indexPath.row]];
    return cell;
}


#pragma mark cellDelegate

-(void)clickMakeAnAppointmentDoctorWithCell:(NewMainCell *)cell
{
    if (![gProjConfig needLogin])
    {
        NSIndexPath * index = [tabMain indexPathForCell:cell];
        NewReservationListViewController * list = [[NewReservationListViewController alloc]initWithNibName:@"NewReservationListViewController" bundle:nil];
        list.doctorData = [doctorList.dataArray objectAtIndex:index.row];
        list.reservationDoctor = YES;
        [self.navigationController pushViewController:list animated:YES];
    }

}



#pragma mark - UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == scrollTop)
    {
        pagev.currentPage = (scrollTop.contentOffset.x/appScreenWidth);
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end









